package com.kantboot.business.ac.listener;

import com.kantboot.business.ac.domain.dto.YrApiToRequestDTO;
import com.kantboot.business.ac.domain.entity.BusAcRechargeProductOrder;
import com.kantboot.business.ac.domain.entity.BusAcYrApiRechargeParam;
import com.kantboot.business.ac.service.IBusAcRechargeProductService;
import com.kantboot.business.ac.service.IBusAcYrApiService;
import com.kantboot.functional.pay.order.event.PayEmitEvent;
import com.kantboot.functional.pay.order.module.dto.FunctionPayOrderDTOOfOrderPaidAfter;
import com.kantboot.functional.pay.order.module.entity.FunctionalPayOrder;
import com.kantboot.functional.pay.order.service.IFunctionalPayOrderService;
import com.kantboot.system.setting.service.ISysSettingService;
import com.kantboot.system.user.service.ISysUserService;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
public class PayEmitByAcRechargeProduct {

    @Resource
    private IBusAcRechargeProductService rechargeProductService;

    @Resource
    private ISysUserService userService;

    @Resource
    private IFunctionalPayOrderService payOrderService;

    @Resource
    private IBusAcYrApiService acYrApiService;

    @Resource
    private ISysSettingService settingService;

    public final static String PRODUCT_CODE = "acRechargeProduct";


    @EventListener
    public void onPayEmitEvent(PayEmitEvent payEmitEvent) {
        FunctionalPayOrder payOrder = payEmitEvent.getPayOrder();
        // 判断是否是ac充值的订单
        if (!PRODUCT_CODE.equals(payOrder.getProductCode())) {
            // 不是ac充值的订单，直接结束
            return;
        }
        // 判断是否充值成功
        if (!FunctionalPayOrder.STATUS_CODE_PAID.equals(payOrder.getStatusCode())) {
            // 不是充值成功的订单，直接结束
            return;
        }

        Double amount = payOrder.getAmount();
        System.err.println(payOrder.getId());
        BusAcRechargeProductOrder orderById = rechargeProductService.getOrderById(payOrder.getId());
        Double amount1
                = rechargeProductService.getAmountByProductId(orderById.getProductId(), payOrder.getUserId());

        if(amount<amount1){
            log.info("充值的订单，金额不够，直接结束");
            FunctionPayOrderDTOOfOrderPaidAfter dto = new FunctionPayOrderDTOOfOrderPaidAfter();
            dto.setId(payOrder.getId());
            dto.setIsPaidAfterCheckSuccess(false);
            dto.setPaidAfterCheckFailReasonCode("amountNotEnough");
            dto.setPaidAfterCheckFailReasonDescription("金额错误");
            payOrderService.handlePaidAfter(dto);
            return;
        }
        log.info("充值的订单，金额够，开始处理");
        HashMap<String, Object> map = new HashMap<>();
        BusAcYrApiRechargeParam apiRechargeParam = orderById.getApiRechargeParam();
        String yrApiNotifyUrl = settingService.getValue("acProjectApi", "yrApiNotifyUrl");
        // 设置回调地址，是yrApi的回调
        apiRechargeParam.setNotifyUrl(yrApiNotifyUrl);

        map.put("product_id", orderById.getApiProductId());
        map.put("out_trade_num", payOrder.getId()+"");
        if (StringUtils.isNotBlank(apiRechargeParam.getMobile())) {
            map.put("mobile", apiRechargeParam.getMobile());
        }
        if (StringUtils.isNotBlank(apiRechargeParam.getNotifyUrl())) {
            map.put("notify_url", apiRechargeParam.getNotifyUrl());
        }
        if (apiRechargeParam.getAmount() != null) {
            map.put("amount", apiRechargeParam.getAmount());
        }
        if (apiRechargeParam.getPrice() != null) {
            map.put("price", apiRechargeParam.getPrice());
        }
        if (StringUtils.isNotBlank(apiRechargeParam.getArea())) {
            map.put("area", apiRechargeParam.getArea());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getYtype())) {
            map.put("ytype", apiRechargeParam.getYtype());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getIdCardNo())) {
            map.put("id_card_no", apiRechargeParam.getIdCardNo());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getCity())) {
            map.put("city", apiRechargeParam.getCity());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getParam1())) {
            map.put("param1", apiRechargeParam.getParam1());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getParam2())) {
            map.put("param2", apiRechargeParam.getParam2());
        }

        if (StringUtils.isNotBlank(apiRechargeParam.getParam3())) {
            map.put("param3", apiRechargeParam.getParam3());
        }

        YrApiToRequestDTO yrApiToRequestDTO = new YrApiToRequestDTO()
                .setUri("/index/recharge")
                .setParam(map);
        acYrApiService.sendRequest(yrApiToRequestDTO);
        log.info("充值的订单，处理完毕");
        FunctionPayOrderDTOOfOrderPaidAfter dto = new FunctionPayOrderDTOOfOrderPaidAfter();
        dto.setId(payOrder.getId());
        dto.setIsPaidAfterCheckSuccess(true);
        payOrderService.handlePaidAfter(dto);

    }

}
